package template;

import java.sql.SQLException;



import java.util.HashMap;
import java.util.LinkedList;

import net.java.ao.DatabaseProvider;
import net.java.ao.EntityManager;

import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.form.DropDownChoice;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.model.PropertyModel;



/**
 * Page that adds a template border for layout.
 * 
 */
public class EditCalendar extends Restricted
{
	//declaration of instance variables
	private String inputNameMessage = "";
	private String selectedCalendar;
	
	//nested private class of form to override onSubmit method
	private class EditCalendarForm extends Form {
		private EditCalendar _editCalendar;
		public EditCalendarForm(String id, EditCalendar editCalendar) {
			super(id);
			_editCalendar = editCalendar;
		}
		public void onSubmit() {
			try {
				_editCalendar.editCalendar();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
    /**
     * Constructor
     */
    public EditCalendar()
    {
    	//propertymodel for the input name
    	PropertyModel inputNameModel = new PropertyModel(this, "inputNameMessage");
    	
    	//hashmap for calendars and collections of their events
    	HashMap<Calendar, LinkedList<Event>> map = new HashMap<Calendar, LinkedList<Event>>();
    	
    	//form instantiation
    	EditCalendarForm form = new EditCalendarForm("form", this);
    	
    	//entitymanager instance
    	EntityManager manager = new EntityManager(DatabaseProvider.getInstance(
				"jdbc:hsqldb:mem:basic-ao", null, null));
    	
    	//list of calendar names for dropdown menu
    	LinkedList<String> calendarNames = new LinkedList<String>();
    	
    	//define user of current session
    	Person user = (Person)_session.getUser();
    	Calendar[] userCalendars;
    	
    	//find all calendars belonging to user, only those created by user can be edited
		try {
			userCalendars = manager.find(Calendar.class, "creatorID = ?", user.getID());
	    	for(int i=3;i<userCalendars.length;i++) {
	    		calendarNames.add(i-3, userCalendars[i].getName());
	    	}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		//propertymodel for the selected calendar
		PropertyModel calendarModel = new PropertyModel(this, "selectedCalendar");
		
		//added dropdown of calendar names and inputfield to form
		form.add(new DropDownChoice("selectedCalendar", calendarModel, calendarNames));
		form.add(new TextField("input", inputNameModel));
		
		//adding the form to the layout
    	_layout.add(form);
    }
    
    public void editCalendar() throws SQLException {
    	
    	String name = inputNameMessage;
    	
    	//entitymanager instance
    	EntityManager manager = new EntityManager(DatabaseProvider.getInstance(
				"jdbc:hsqldb:mem:basic-ao", null, null));
    	
    	//define user of current session
    	Person user = (Person)_session.getUser();
    	
    	//if the name field is empty, redirect to error page
    	if(name == null) {
    		this.setResponsePage(EditCalendarError.class);
    		return;
    	}
    	
    	//find the calendar the user selected
    	Calendar[] calendar = manager.find(Calendar.class, "creatorID = ? AND name = ?", user.getID(), selectedCalendar);
    	
    	//retrieve all of the user's calendars
    	Calendar[] userCalendars = manager.find(Calendar.class, "creatorID = ?", user.getID());
    	
    	//if the input calendar name already exists for a user, redirect to error page
    	for(int i=0;i<userCalendars.length;i++) {
    		String tempName = userCalendars[i].getName();
    		if(tempName.equalsIgnoreCase(name)) {
    			this.setResponsePage(EditCalendarError.class);
    			return;
    		}
    	}
    	
    	//set the calendar's name to the input name and save it
    	calendar[0].setName(name);
    	calendar[0].save();
    	
    	//redirect to view calendar page
    	this.setResponsePage(ViewCalendars.class);
    }
}